<?
ob_start();
include("common/header.html");
include("common/left.html");
$out_html=ob_get_contents();
ob_end_clean();
include("common/page.php");
if($_SESSION['memberID']!=1 && !($_SESSION['sid']==1 && strpos($_SESSION['qx'],'refund'))){
    exit("无访问权限");
}
$currentPage=$_REQUEST["currentPage"]?$_REQUEST["currentPage"]:1;
$pageSize=$_REQUEST['pageSize']?$_REQUEST["pageSize"]:10;
$offset=($currentPage-1)*$pageSize;
$r=array();
//用户身份判断
$pos_list = array(
    '客服部',
    '市场部',
    '财务部',
    '技术部',
    '其他部门',
);

//$draw_status = array(
//    0=>'申请提现',
//    1=>'同意提现',
//    2=>'成功提现',
//    3=>'取消提现',
//    4=>'拒绝提现',
//);
$btn_ok         = '同意';
$btn_deny       = '拒绝';
if ($_SESSION['memberID']!=1) {
    $sql = "SELECT position FROM pft_member_extinfo WHERE fid={$_SESSION['memberID']} LIMIT 1";
    $GLOBALS['le']->query($sql);
    $GLOBALS['le']->fetch_assoc();
    $position = $GLOBALS['le']->f('position');
    if ($_SESSION['memberID']!=1 && $position=='') {
        exit('您的账号还未指定所在部门，请先联系管理员设置');
    }
//TODO::test
//$position = $_GET['pos'] ? $_GET['pos'] : 0;
    switch ($position) {
        case 0:
            $wd_status      = '0';
            $status_ok      = 1;
            $status_deny    = 4;
            $btn_ok         = '同意';
            $btn_deny       = '拒绝';
            break;
        case 2:
            $wd_status      = '0,1,2,3,4';
            $status_ok      = 2;
            $status_deny    = 0;//
            $btn_ok         = '转账';
            $btn_deny       = '撤回';
            break;
        default:
            $disable_ctr    = $_SESSION['memberID']!=1 ? true : false;//禁止操作
            break;
    }
}

$where = " WHERE 1=1";
if($_SESSION['account']=='csj1') $wd_status = '0,1,2,3,4';
if (isset($wd_status)) $where .= " AND wd_status in ($wd_status)";
if($dname=mysql_real_escape_string(trim($_REQUEST['dname']))){
	$where .=" and m.dname like '%$dname%'";
}
if($mid=mysql_real_escape_string(trim($_REQUEST['mid']))){
		$where .=" and m.id=$mid";
}
$time=$_REQUEST['time'];
if($_REQUEST['btime']){
	$where.=" and w.$time>='".mysql_real_escape_string($_REQUEST['btime'])." 00:00:00'";
}
if($_REQUEST['etime']){
	$where.=" and w.$time<='".mysql_real_escape_string($_REQUEST['etime'])." 23:59:59'";
}

// 导出
if($_REQUEST['act']=='Export'){
	$offset = 0 ;
	$pageSize = 9999999;
}

$str="select w.*,m.dname,m.id as memberID,m.fee_pay_way,m.account,wd_time,e.com_name"
    ." from pft_wd_cash w left join pft_member m on m.id=w.fid left join pft_member_extinfo e on m.id=e.fid"
    ." $where  order by w.wd_status ASC,w.apply_time DESC limit $offset,$pageSize";
$GLOBALS['le']->query($str);
if($_SESSION['account']=='csj1') echo $str;
while($row=$GLOBALS['le']->fetch_assoc()){

//wd_money 实际从他的余额账户中扣掉的金额
    if($row['apply_time']>"2015-03-01 00:00:00"){
        if($row['fee_bank_once']==0){
            $row['tfee']=0;
        }elseif($row['cut_fee_way']==1){
            //从余额中扣 手续费是算在wd_money里面了
            $row['tfee'] = (($row['wd_money']/(1000+$row['fee_bank_once']))*$row['fee_bank_once'])/100;
            $row['tfee'] =  $row['tfee']<1?1: $row['tfee'];
        }else{//$row['cut_fee_way']==0
            //从提现金额中扣
            $row['tfee'] = $row['wd_money']*$row['fee_bank_once']/100000 < 1? 1 : round($row['wd_money']*$row['fee_bank_once']/100000);
        }
    }else{        
        $row['tfee'] = $row['wd_money']<40000 ? 2 : $row['wd_money']/20000;
    }
    $row['tfee'] = strpos($row['tfee'] ,'.') ? substr($row['tfee'],0,strpos($row['tfee'],'.')) : $row['tfee'];
    $r[] = $row; 
}

//print_r($r[0]);
$wd_statusT=array(
    0=>'申请提现',
    1=>'同意提现',
    2=>'成功提现',
    3=>'取消提现',
    4=>'拒绝提现',
);
// 导出
if(isset($_REQUEST['act']) && $_REQUEST['act']=="Export")
{
	$Excel = array();
	$filename=date('YmdHis').'提现申请列表';
	$Excel[0]['a']='银行名称/收款账号(户名)';
	$Excel[0]['b']='金额(元)';
	$Excel[0]['c']='手续费';
	$Excel[0]['d']='转账金额';
	$Excel[0]['e']='处理人员';
	$Excel[0]['f']='申请时间';
	$Excel[0]['g']='备注';
	$Excel[0]['h']='处理进度';
	$i=1;
	foreach($r as $lid=>$row){
		$n++;
		$Excel[$n]['a']=$row['bank_name'].'：'.$row['bank_accuont'];
		$Excel[$n]['b']=$row['wd_money']/100;
		$Excel[$n]['c']=floor($row['tfee']/100);
		$Excel[$n]['d']=$row['wd_money']/100-floor($row['tfee']/100);
		$Excel[$n]['e']=$row['wd_operator']?$row['wd_operator']:'管理员';
		$Excel[$n]['f']=$row['apply_time'];
		$Excel[$n]['g']=$row['memo'];
		$Excel[$n]['h']=$wd_statusT[$row['wd_status']];
	}
	$xls = new SimpleExcel('UTF-8', true,'orderList');
    $xls->addArray($Excel);
    $xls->generateXML($filename);
	exit;
}

echo $out_html;
$str="select count(*) as total from pft_wd_cash w left join pft_member m on m.id=w.fid $where";
$GLOBALS['le']->query($str);
$GLOBALS['le']->fetch_assoc();
$total=$GLOBALS['le']->f('total');
$wd_statusT=array(
	0=>"<font color='red'>未处理</font>",
	1=>"<font color='orange'>同意提现</font>",
	2=>"<font color='green'>成功提现</font>",
	3=>"<font color='gray'>取消提现</font>",
	4=>"<font color='blue'>拒绝提现</font>",
);
include("tpl/refund.tpl");
include("common/footer.html");
?>
